﻿package mortal.game.scene3D.layer3D.utils
{
    import baseEngine.system.*;
    import com.gengine.utils.*;
    import flash.geom.*;
    import frEngine.core.*;
    import frEngine.math.*;
    import mortal.game.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.map3D.*;

    public class Scene3DUtil extends Object
    {
        public static var _cameraAngle:Number = 40;
        public static var anglePI:Number = cameraAngle / 180 * Math.PI;
        public static var cameraAngleCos:Number = Math.cos(anglePI);
        public static var cameraAngleSin:Number = Math.sin(anglePI);
        private static var to3D:Number = 1 / cameraAngleSin;
        private static var _gameCameraCopy:OrthographicCamera3D;
        private static var _UICamera:OrthographicCamera3D;
        private static var _cameraQuaternion:Quaternion;
        private static var _tempPoint:Point = new Point();

        public function Scene3DUtil()
        {
            return;
        }// end function

        public static function set cameraAngle(param1:Number) : void
        {
            _cameraAngle = param1;
            anglePI = _cameraAngle / 180 * Math.PI;
            cameraAngleCos = Math.cos(anglePI);
            cameraAngleSin = Math.sin(anglePI);
            to3D = 1 / cameraAngleSin;
            Game.scene.gameCamera.rotationX = Scene3DUtil.cameraAngle;
            Game.scene.gameCamera.y = 1000;
            Game.scene.gameCamera.setScreenPos(SceneRange.displayInt);
            return;
        }// end function

        public static function get cameraAngle() : Number
        {
            return _cameraAngle;
        }// end function

        public static function getSceneMousePostion(param1:Number, param2:Number, param3:Boolean) : Point
        {
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_4:* = SceneRange.sceneScale;
            var _loc_5:* = Device3D.viewPortW * 0.5;
            var _loc_6:* = Device3D.viewPortH * 0.5;
            _tempPoint.x = (param1 - _loc_5) / _loc_4 + _loc_5;
            _tempPoint.y = (param2 - _loc_6) / _loc_4 + _loc_6;
            if (param3)
            {
                _loc_7 = GameCamera(Game.scene.camera).x2d;
                _loc_8 = GameCamera(Game.scene.camera).y2d;
                _tempPoint.x = _tempPoint.x + _loc_7;
                _tempPoint.y = _tempPoint.y + _loc_8;
            }
            return _tempPoint;
        }// end function

        public static function get gameCameraCopy() : OrthographicCamera3D
        {
            if (!_gameCameraCopy)
            {
                _gameCameraCopy = new OrthographicCamera3D("gameCameraCopy");
                _gameCameraCopy.parent = Global3D.scene;
                _gameCameraCopy.x = 0;
                _gameCameraCopy.y = 0;
                _gameCameraCopy.z = -1000;
                _gameCameraCopy.zoom = 1;
                _gameCameraCopy.updateProjectionMatrix();
            }
            return _gameCameraCopy;
        }// end function

        public static function get UICamera() : OrthographicCamera3D
        {
            if (!_UICamera)
            {
                _UICamera = new OrthographicCamera3D("gameCameraCopy");
                _UICamera.parent = Global3D.scene;
                _UICamera.x = 0;
                _UICamera.y = 2000;
                _UICamera.z = -1000;
                _UICamera.zoom = 1;
                _UICamera.updateProjectionMatrix();
            }
            return _UICamera;
        }// end function

        public static function get cameraQuaternion() : Quaternion
        {
            if (!_cameraQuaternion)
            {
                _cameraQuaternion = new Quaternion();
                _cameraQuaternion.fromAxisAngle(Vector3D.X_AXIS, cameraAngle / 180 * Math.PI);
            }
            return _cameraQuaternion;
        }// end function

        public static function change3Dto2D(param1:Vector3D) : Point
        {
            _tempPoint.x = param1.x;
            _tempPoint.y = (-param1.z) * cameraAngleSin - param1.y * cameraAngleCos;
            return _tempPoint;
        }// end function

        public static function change2Dto3DY(param1:Number) : Number
        {
            return (-param1) * to3D;
        }// end function

        public static function change2Dto3DX(param1:Number) : Number
        {
            return param1;
        }// end function

        public static function changeMap2dToScene3d(param1:Number, param2:Number) : Vector3D
        {
            return new Vector3D(param1, 0, (-param2) * to3D);
        }// end function

        public static function change2Dto3DRotation(param1:Number) : Number
        {
            var _loc_2:* = MathUitl.getRadians(param1);
            var _loc_3:* = Math.cos(_loc_2);
            var _loc_4:* = Math.sin(_loc_2);
            var _loc_5:* = change2Dto3DY(_loc_4);
            var _loc_6:* = MathUitl.getRadiansByXY(0, 0, _loc_5, _loc_3);
            var _loc_7:* = MathUitl.getAngle(_loc_6) - 180;
            return _loc_7;
        }// end function

    }
}
